<?php
require_once(TIXING_PATH . '/functions.php');

function tixing_admin() {
    add_options_page('动态提醒', '动态提醒','manage_options', __FILE__, 'tixing_page');
}
function tixing_page(){
    ?>
    <div class="wrap">

    <h2>动态提醒</h2>

    <?php
    //加载配置
    $option = tixing_get_option();
    if(isset($_POST['tixing_save']) && check_admin_referer('tixing_setting', 'save')){
        $hook_save = (isset($_POST['tixing_hook']) ? ($_POST['tixing_hook']) : ([]));
        $hook['user_signup'] = (in_array('user_signup', $hook_save)) ? (1) : (0);
        $hook['comment_post'] = (in_array('comment_post', $hook_save)) ? (1) : (0);
        $option['hook'] = $hook;

        if(isset($_POST['channel_code'])){
            $option['channel_code'] = trim( $_POST['channel_code'] );
        }
        tixing_update_option($option);
    }
    $channel_code = tixing_get_array_value_by_key($option, 'channel_code', null);
    $hook = tixing_get_array_value_by_key($option, 'hook', []);
    $hook_user_signup = tixing_get_array_value_by_key($hook, 'user_signup', 1);
    $hook_comment_post = tixing_get_array_value_by_key($hook, 'comment_post', 1);
    ?>

    <div class="wrap">
        <h1>设置</h1>
        <form method="post" action="" novalidate="novalidate">
            <input type="hidden" name="tixing_save" value="save" />
            <table class="form-table">

                <tr>
                    <th scope="row"><label for="channel_code">口令</label></th>
                    <td><input name="channel_code" type="text" id="channel_code" value="<?php echo esc_html($channel_code); ?>" class="regular-text" />
                        <p class="description" id="channel_code-description">关注微信公众号：<a href="javascript:document.getElementById('tixing-qrcode').style.display='block';void(0);">【一封传话】</a>，在 <a href="https://www.phprm.com/push/h5/todo_create.html">口令提醒</a> 点击图标创建一个口令提醒后，把提醒的口令填写到这里，后续可以到微信公众号——个人中心配置钉钉、飞书等推送方式。</p>
                        <p id="tixing-qrcode" style="display:none;"><img src=""></p>
                    </td>
                </tr>

                <tr>
                    <th scope="row">在这些时候发送通知提醒</th>
                    <td>
                        <fieldset><legend class="screen-reader-text"><span>在这些时候发送通知提醒</span></legend>
                            <label for="tixing_hook_user_signup">
                                <input name="tixing_hook[]" type="checkbox" id="tixing_hook_user_signup" value="user_signup" <?php echo ($hook_user_signup === 1)?('checked'):(''); ?> />
                                有新用户注册时
                            </label>
                        </fieldset>
                        <label for="tixing_hook_comment_post">
                            <input name="tixing_hook[]" type="checkbox" id="tixing_hook_comment_post" value="comment_post" <?php echo ($hook_comment_post === 1)?('checked'):(''); ?> />
                            有新的评论提交时
                        </label>
                        </fieldset>
                    </td>
                </tr>


            </table>
            <?php wp_nonce_field( 'tixing_setting', 'save' ); ?>
            <input type="submit" class="button-primary" value="保存" />
        </form>
    </div>
    <?php
}
add_action('admin_menu', 'tixing_admin');

